﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using BusinessLogicLayer;
using IBusinessLogicLayer;
using Entity;
using Microsoft.AspNet.Identity;
using MVC.ViewModels;

namespace MVC.Controllers
{
    public class ShoppingCartController : Controller
    {
        private IShoppingCartBll shoppingCartBll;
        private IBaseBll<Album> albumBll; 

        public ShoppingCartController(IShoppingCartBll shoppingCartBll,IBaseBll<Album> albumBll)
        {
            this.shoppingCartBll = shoppingCartBll;
            this.albumBll = albumBll;
        }
        // GET: ShoppingCart
        public ActionResult Index()
        {
            var shoppingCart = shoppingCartBll.GetCarts(GetCurrentUserId());
            var viewModel = new ShoppingCartListVm()
            {
                Carts = shoppingCart.ToList(),
                CartTotal = shoppingCartBll.GetTotal(GetCurrentUserId())
            };
            return View(viewModel);
        }

        public ActionResult AddToCart(int id)
        {
            var addAlbum = albumBll.FindSingle(c => c.Id == id);
            var cartItem = new Cart()
            {
                AlbumId = addAlbum.Id,
                Count = 1,
                DateCreated = DateTime.Now,
                UserId = GetCurrentUserId()
            };
            shoppingCartBll.AddCart(cartItem);
            return RedirectToAction("Index");
        }

        public ActionResult RemoveFromCart(int id)
        {
            var cartItem = new Cart()
            {
                AlbumId = id,
                UserId = GetCurrentUserId()
            };
            shoppingCartBll.RmoveFromCart(cartItem);
            return RedirectToAction("Index");
        }
        [ChildActionOnly]
        public ActionResult CartBage()
        {
            var count = shoppingCartBll.GetCount(GetCurrentUserId());
            ViewBag.Count = count == 0 ? "" : count.ToString();
            return PartialView();
        }

        // GET: ShoppingCart/Details/5
        public ActionResult Details(int id)
        {
            return View();
        }

        // GET: ShoppingCart/Create
        public ActionResult Create()
        {
            return View();
        }

        // POST: ShoppingCart/Create
        [HttpPost]
        public ActionResult Create(FormCollection collection)
        {
            try
            {
                // TODO: Add insert logic here

                return RedirectToAction("Index");
            }
            catch
            {
                return View();
            }
        }

        // GET: ShoppingCart/Edit/5
        public ActionResult Edit(int id)
        {
            return View();
        }

        // POST: ShoppingCart/Edit/5
        [HttpPost]
        public ActionResult Edit(int id, FormCollection collection)
        {
            try
            {
                // TODO: Add update logic here

                return RedirectToAction("Index");
            }
            catch
            {
                return View();
            }
        }

        // GET: ShoppingCart/Delete/5
        public ActionResult Delete(int id)
        {
            return View();
        }

        // POST: ShoppingCart/Delete/5
        [HttpPost]
        public ActionResult Delete(int id, FormCollection collection)
        {
            try
            {
                // TODO: Add delete logic here

                return RedirectToAction("Index");
            }
            catch
            {
                return View();
            }
        }

        private string GetCurrentUserId()
        {
            return HttpContext.User.Identity.GetUserId();
        }
    }
}
